package com.fr.chicken.manager.verifcode;

import com.fr.chicken.util.SmsUtils.SendSms;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

public class SMSVerifyCode extends AbstractVerifyCode<String> {
    private final static long VALID_TIME = 1;

    private static class SMSVerifyCodeSingle{
        private static final SMSVerifyCode _instance = new SMSVerifyCode();
    }

    public static SMSVerifyCode getInstance(){
        return SMSVerifyCodeSingle._instance;
    }

    private SMSVerifyCode(){
        super(VALID_TIME,TimeUnit.MINUTES);
    }
    @Override
    protected String createVerifyCodeStr() {
        return VerifyCodeGenerateFactory.getVerifyCodeGenerate().getVerifyCode();
    }

    @Override
    protected VarcharCode createAndSaveVerifyCode(String key) {
        VarcharCode result =  super.createAndSaveVerifyCode(key);
        sendSMS(key,result.getVerifyCode());
        return result;
    }

    /**
     * 给用户发送验证码
     * */
    private void sendSMS(String phone,String code){
      try {
           SendSms.sendSms(phone,code);
       } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
